#商城主系统蓝图
from flask import Blueprint,render_template,request
from models import Product
bp=Blueprint("centre",__name__,url_prefix="/")

#bannner部分热门推荐函数，返回三个热门商品
def hot_recommend():
    product_list = Product.query.order_by(Product.sales.desc()).limit(3)
    return product_list

#猜你喜欢部分推荐函数，返回10个个性化推荐商品
def personalized_recommend():
    product_list = Product.query.order_by(Product.sales.desc()).limit(10)
    return product_list

#销量最好的8个商品  偶数个商品
def top_sell_recommend():
    product_list = Product.query.order_by(Product.sales.desc()).limit(8)
    return product_list

#首页
@bp.route("/")
def index():
    banner_product_list = hot_recommend()
    personalized_product_list = personalized_recommend()
    top_sell_product_list = top_sell_recommend()
    return render_template("index.html", banner_product_list=banner_product_list, personalized_product_list=personalized_product_list, top_sell_product_list=top_sell_product_list)

#相似推荐  推荐3个与当前商品相似的商品
def similar_recommend():
    product_list = Product.query.order_by(Product.sales.desc()).limit(3)
    return product_list

#商品详情页面
@bp.route("/detail/<pid>")#这里更改url,还不确定怎么搞
def detail(pid):
    product = Product.query.get(pid)
    similar_product_list = similar_recommend()
    return render_template("detail.html", product=product, similar_product_list=similar_product_list)


#搜索页
@bp.route("/search", methods=['POST'])
def search():
    # return render_template("search_result.html")
    keyword = request.form.get('keyword')
    search_product_list = Product.query.filter(Product.name.like("%"+keyword+"%")if keyword is not None else "" ).limit(100)
    return render_template("search_result.html", search_product_list=search_product_list)



# #分类搜索页
# @bp.route("/url")#这里更改url,还不确定怎么搞
# def classfication():
#     return render_template("classfication_recommend.html")


